package com.chen.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

// 线程池的三大方法
public class ThreadPoolMethods {
    public static void main(String[] args) {

        // 1、线程池大小为单个线程
        // ExecutorService threadPool = Executors.newSingleThreadExecutor();
        // 2、线程池大小为固定数量的线程数
        // ExecutorService threadPool = Executors.newFixedThreadPool(5);
        // 3、线程池大小为可变的线程数量
        ExecutorService threadPool = Executors.newCachedThreadPool();

        try {
            for (int i = 0; i < 10; i++) {
                // execute方法，执行线程
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "=》hi");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 线程池用完需要关闭
            threadPool.shutdown();
        }

    }
}
